import { CustomTableQuestion, InputQuestion, QuestionBase, SelectQuestion, TextQuestion, TitleQuestion } from '@/components/public-template';
import { TextTable } from '@/components/public-template';
import { TableBase } from '@/components/public-template';
import { regExp } from '@/utils/regExp';
import { Api } from '@/config/api/projectFile';
import { Api as ApiStatement } from '@/config/api/watermark';
import { Api as ApiSystem } from '@/config/api/system';
import { Api as ApiFile } from '@/config/api/fileInfo';
import { CATEGORIZE_FROM } from '@/enums/commonEnum';
import { changeByte } from '@/utils/common';

class HomeService {
  getQuestions() {
    const questions: QuestionBase<any>[] = [
      new InputQuestion({
        key: 'name',
        label: '名称',
        value: null,
        state: {
          name: 'searchName'
        }
      }),
      new TextQuestion({
        key: 'parentId',
        hide: true,
        value: null
      })
    ];
    return questions;
  }
  // 添加文件夹
  getFileQuestions() {
    const questions: QuestionBase<any>[] = [
      new InputQuestion({
        key: 'name',
        label: '文件夹名称',
        required: true,
        prompt: '请输入，长度不可超过120个字符',
        reg: regExp.len120,
        value: null
      }),
      new SelectQuestion({
        key: 'watermarkingId',
        value: null,
        label: '电子水印',
        type: 'search',
        state: {
          url: ApiStatement.watermarkPage,
          protType: 'post',
          nameKey: 'name',
          nameValue: 'id',
          params: {
            pageNumber: 1,
            pageSize: 999
          }
        }
      }),
      new InputQuestion({
        key: 'desc',
        label: '描述',
        value: null,
        type: 'textarea',
        state: { grid: 24 },
        reg: regExp.len200,
        prompt: '长度不可超过200个字符'
      }),
      new TitleQuestion({
        key: 'title',
        label: '权限分配'
        // state: { noShrink: true }
      }),
      new CustomTableQuestion({
        key: 'projectFilePermissionDTOS',
        value: [],
        state: {
          default: {
            // permission: 2,
            // memberIds: ''
          },
          grid: 24,
          fullLine: true,
          isAdd: true,
          hideBorder: true,
          columns: [
            {
              title: '序号',
              type: 'index'
            },
            {
              title: '权限',
              key: 'permission',
              type: 'select',
              // required: true,
              // prompt: '请选择权限类型',
              state: {
                filterName: 'FILE_PERMISSION'
              }
            },
            {
              title: '员工',
              key: 'memberIds',
              type: 'tree-select',
              // prompt: '请选择员工用户',
              // required: true,
              width: 300,
              state: {
                url: ApiSystem.orgTreeDataSubject,
                treeMultiple: true,
                nameKey: 'name',
                nameValue: 'id',
                nameChildren: 'childOrg',
                protType: 'post',
                childrenLevel: {
                  nameKey: 'nickName',
                  nameValue: 'id',
                  nameChildren: 'subjectSimpleList'
                }
              }
            }
          ]
        }
      })
    ];
    return questions;
  }
  getTables() {
    const tables: TableBase<any>[] = [
      new TextTable({
        key: 'key',
        value: null,
        type: 'slot',
        columns: { width: 60 },
        state: {
          slotName: 'fileImg'
        }
      }),
      new TextTable({
        key: 'name',
        value: '文件名称',
        type: 'slot',
        state: {
          slotName: 'fileName'
        },
        columns: {
          width: 300,
          sorter: (a, b) => {
            return a.name.localeCompare(b.name);
          }
        }
      }),
      new TextTable({
        key: 'size',
        value: '大小',
        type: 'slot',
        state: {
          slotName: 'size'
        },
        columns: {
          width: 100,
          sorter: (a, b) => {
            return b.size - a.size;
          }
        }
      }),
      new TextTable({
        key: 'permissionConfiguration',
        value: '权限',
        type: 'slot',
        state: {
          slotName: 'authority'
        },
        columns: {
          sorter: (a, b) => {
            return a.permissionConfiguration.localeCompare(b.permissionConfiguration);
          }
        }
      }),
      new TextTable({
        key: 'modifierName',
        value: '修改人',
        type: 'slot',
        state: {
          slotName: 'modifier'
        },
        columns: {
          sorter: (a, b) => {
            return a.modifierName.localeCompare(b.modifierName);
          }
        }
      }),
      new TextTable({
        key: 'modifiedTime',
        value: '修改时间',
        type: 'slot',
        state: {
          slotName: 'updateTime'
        },
        columns: {
          sorter: (a, b) => {
            return Date.parse(a.modifiedTime) - Date.parse(b.modifiedTime);
          }
        }
      }),
      new TextTable({
        key: 'actions',
        value: '操作',
        columns: { width: 150 },
        handle: [
          {
            type: 'local',
            text: '详情',
            class: 'text-primary',
            state: {},
            showKey: [{ key: 'folder', value: true }]
          },
          {
            type: 'local',
            text: '下载',
            class: 'text-primary',
            state: {},
            showKey: [
              {
                key: 'folder',
                value: false
              },
              { key: 'canDownload', value: true }
            ]
          },
          {
            text: '更多',
            class: 'text-primary',
            showKey: [
              { key: 'folder', value: true },
              { key: 'moreBtn', value: true }
            ],
            state: {
              select: true,
              options: [
                {
                  type: 'layerFrom',
                  label: '重命名',
                  params: [{ id: 'id' }],
                  showKey: [{ key: 'canEdit', value: true }],
                  state: {
                    form: this.getFolderRenameQuestions(),
                    config: {
                      title: '重命名',
                      name: 'fileRenaming',
                      width: '390px',
                      type: 'layer',
                      grid: 24,
                      labelCol: 24,
                      wrapperCol: 24,
                      layout: 'vertical',
                      addUrl: Api.projectRename,
                      dataParams: [{ OLD_FOLDER_NAME: 'name', name: 'name' }],
                      combinationMethod: (data) => {
                        delete data.OLD_FOLDER_NAME;
                        return data;
                      }
                    }
                  }
                },
                {
                  type: 'local',
                  label: '下载',
                  showKey: [{ key: 'canDownload', value: true }],
                  state: {}
                },
                {
                  type: 'delete',
                  label: '删除',
                  url: Api.projectDelete,
                  showKey: [{ key: 'canDelete', value: true }],
                  postType: 'post',
                  urlName: 'id',
                  state: {}
                },
                {
                  type: 'layerFrom',
                  label: '复制到',
                  params: [{ formId: 'id' }],
                  showKey: [{ key: 'canCopy', value: true }],
                  state: {
                    form: this.getCopyQuestions('复制到', 'folder'),
                    params: { categorizeFrom: CATEGORIZE_FROM.PROJECT },
                    config: {
                      title: '复制',
                      name: 'folderCopying',
                      width: '390px',
                      type: 'layer',
                      grid: 24,
                      labelCol: 24,
                      wrapperCol: 24,
                      localSub: true
                    }
                  }
                },
                {
                  type: 'layerFrom',
                  label: '移动到',
                  params: [{ formId: 'id' }],
                  showKey: [{ key: 'canMove', value: true }],
                  state: {
                    form: this.getCopyQuestions('移动到', 'folder'),
                    params: { categorizeFrom: CATEGORIZE_FROM.PROJECT },
                    config: {
                      title: '移动',
                      name: 'folderMoving',
                      width: '390px',
                      type: 'layer',
                      grid: 24,
                      labelCol: 24,
                      wrapperCol: 24,
                      localSub: true
                    }
                  }
                }
              ]
            }
          },
          {
            text: '更多',
            class: 'text-primary',
            showKey: [{ key: 'folder', value: false }],
            state: {
              select: true,
              options: [
                {
                  type: 'layerFrom',
                  label: '重命名',
                  params: [{ id: 'id' }],
                  showKey: [{ key: 'canEdit', value: true }],
                  state: {
                    form: this.getRenameQuestions(),
                    config: {
                      title: '重命名',
                      name: 'fileRenaming',
                      width: '390px',
                      type: 'layer',
                      grid: 24,
                      labelCol: 24,
                      wrapperCol: 24,
                      layout: 'vertical',
                      addUrl: Api.fileRename,
                      dataParams: [{ OLD_NAME: 'name' }],
                      combinationMethod: (data) => {
                        delete data.OLD_NAME;
                        return data;
                      }
                    }
                  }
                },
                {
                  type: 'layerTable',
                  label: '历史版本',
                  class: 'text-primary',
                  params: [{ fileId: 'id' }],
                  state: {
                    table: this.getVersionTables(),
                    config: {
                      name: 'projectFileVersion',
                      title: '历史版本',
                      type: 'layer',
                      queryUrl: Api.versionPage,
                      protType: 'post',
                      cancelSubmit: true,
                      cancelText: '返回'
                    }
                  }
                },
                {
                  type: 'delete',
                  label: '删除',
                  showKey: [{ key: 'canDelete', value: true }],
                  url: Api.fileDelete,
                  postType: 'post',
                  urlName: 'id',
                  state: {}
                },
                {
                  type: 'layerFrom',
                  label: '复制到',
                  params: [{ formId: 'id' }],
                  showKey: [{ key: 'canCopy', value: true }],
                  state: {
                    form: this.getCopyQuestions('复制到', 'file'),
                    params: { categorizeFrom: CATEGORIZE_FROM.PROJECT },
                    config: {
                      title: '复制',
                      name: 'fileCopying',
                      width: '390px',
                      type: 'layer',
                      grid: 24,
                      labelCol: 24,
                      wrapperCol: 24,
                      localSub: true
                    }
                  }
                },
                {
                  type: 'layerFrom',
                  label: '移动到',
                  showKey: [{ key: 'canMove', value: true }],
                  params: [{ formId: 'id' }],
                  state: {
                    form: this.getCopyQuestions('移动到', 'file'),
                    params: { categorizeFrom: CATEGORIZE_FROM.PROJECT },
                    config: {
                      title: '移动',
                      name: 'fileMoving',
                      width: '390px',
                      type: 'layer',
                      grid: 24,
                      labelCol: 24,
                      wrapperCol: 24,
                      localSub: true
                    }
                  }
                },
                {
                  type: 'layerTable',
                  label: '任务记录',
                  class: 'text-primary',
                  params: [{ fileId: 'id' }],
                  state: {
                    table: this.getRecordTables(),
                    config: {
                      title: '任务记录',
                      type: 'layer',
                      queryUrl: Api.recordPage,
                      protType: 'post',
                      cancelSubmit: true,
                      cancelText: '返回'
                    }
                  }
                }
              ]
            }
          }
        ]
      })
    ];
    return tables;
  }
  getIcons() {
    const icons: TableBase<any>[] = [
      new TextTable({
        key: 'layerFrom',
        value: '添加文件夹',
        type: 'primary',
        class: 'icon-add',
        state: {
          form: this.getFileQuestions(),
          config: {
            type: 'layer',
            name: 'projectFile',
            title: '添加项目文件夹',
            width: '390px',
            grid: 24,
            labelCol: 24,
            wrapperCol: 24,
            layout: 'vertical',
            localSub: true
          }
        }
      }),
      new TextTable({
        key: 'upload',
        value: '上传',
        type: 'default',
        class: 'icon-cloud-upload',
        state: {
          multiple: true,
          category: CATEGORIZE_FROM.PROJECT,
          sizeTips: '该文件已超过500M，请使用客户端上传',
          size: '500', //单位 M
          autoUploadFileServer: true,
          hideOkButton: true,
          downloadRule: '支持扩展名：.rar .zip .doc .docx .pdf .jpg...',
          params: { directoryId: '' }
        }
      }),
      new TextTable({
        key: 'local',
        value: '下载',
        type: 'default',
        class: 'icon-download',
        state: {}
      }),
      new TextTable({
        key: 'local',
        value: '删除',
        type: 'default',
        class: 'icon-delete',
        state: {}
      })
    ];
    return icons;
  }
  // 文件/目录-复制到/移动到
  getCopyQuestions(label, form) {
    const questions: QuestionBase<any>[] = [
      new SelectQuestion({
        key: 'toDirectoryId',
        label,
        required: true,
        prompt: '请选择',
        value: null,
        type: 'tree',
        state: {
          fullPath: true,
          url: ApiFile.fileGetFoldersWithPriv,
          protType: 'post',
          nameKey: 'name',
          nameValue: 'id',
          nameChildren: 'children',
          params: form === 'file' ? { uploadPriv: true } : { manager: true },
          changeParams: [{ categorizeTo: 'categorize' }]
        }
      })
    ];
    return questions;
  }
  // 文件夹重命名
  getFolderRenameQuestions() {
    const questions: QuestionBase<any>[] = [
      new InputQuestion({
        key: 'name',
        label: '新文件夹名称',
        required: true,
        prompt: '请输入，长度不可超过120个字符',
        reg: regExp.len120,
        value: null
      })
    ];
    return questions;
  }
  // 文件重命名
  getRenameQuestions() {
    const questions: QuestionBase<any>[] = [
      new InputQuestion({
        key: 'name',
        label: '新文件名称',
        required: true,
        prompt: '请输入，长度不可超过120个字符',
        reg: regExp.len120,
        value: null
      })
    ];
    return questions;
  }
  // 历史版本
  getVersionTables() {
    const tables: TableBase<any>[] = [
      new TextTable({
        key: 'name',
        value: '文件名称'
      }),
      new TextTable({
        key: 'size',
        value: '大小',
        type: 'custom',
        columns: {
          customRender: (data: any) => {
            return changeByte(data.value);
          }
        }
      }),
      new TextTable({
        key: 'version',
        value: '版本'
      }),
      new TextTable({
        key: 'typeName',
        value: '操作记录'
      }),
      new TextTable({
        key: 'modifiedTime',
        value: '修改时间'
      }),
      new TextTable({
        key: 'actions',
        value: '操作',
        handle: [
          {
            type: 'local',
            text: '回滚到该版本',
            class: 'text-primary',
            // url: '/dmsfile/personFileVersion/setAsCurrent',
            // postType: 'post',
            // urlName: 'id',
            // params: [{ id: 'id' }],
            showKey: [{ key: 'currentVersion', value: 0 }],
            state: {
              name: 'VERSION_BACK'
              // hintData: {
              //   title: '原文件将会被覆盖，是否确定回滚该文件？'
              // }
            }
          },
          {
            type: 'local',
            text: '下载',
            class: 'text-primary',
            state: {
              name: 'VERSION_DOWN'
            }
          }
        ]
      })
    ];
    return tables;
  }
  // 任务记录
  getRecordTables() {
    const tables: TableBase<any>[] = [
      new TextTable({
        key: 'name',
        value: '文件名称'
      }),
      new TextTable({
        key: 'size',
        value: '大小',
        type: 'custom',
        columns: {
          customRender: (data: any) => {
            return changeByte(data.value);
          }
        }
      }),
      new TextTable({
        key: 'version',
        value: '版本'
      }),
      new TextTable({
        key: 'optName',
        value: '类型'
      }),
      new TextTable({
        key: 'status',
        value: '状态',
        type: 'dictionary',
        state: {
          filterName: 'TASK_RECORD_STATUS'
        }
      }),
      new TextTable({
        key: 'creatorName',
        value: '创建人'
      }),
      new TextTable({
        key: 'createdTime',
        value: '创建时间'
      })
    ];
    return tables;
  }
}

export default new HomeService();
